****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************

clear all 
set more off

use choice-school-xwalk.dta , clear 
global prog "SchoolCode Prog_type" 
rename Choice appch1 
keep appch1 SchoolCode Prog_type 

foreach x in $prog { 
	rename `x' `x'1 
	} 

tempfile appch1 
	save `appch1' , replace 

foreach x in appch $prog { 
	rename `x'1 `x'2 
	} 
	
tempfile appch2 
	save `appch2' , replace 
	
foreach x in appch $prog { 
	rename `x'2 `x'3 
	} 
	
tempfile appch3 
	save `appch3' , replace 
	
	
** lottery data
	
foreach year in 06 07 08 09 10 11 12 13 { 

use cms20`year'_lot.dta, clear 

keep if mastid!=. 

cap noisily gen grade = real(futuregrade) 
cap noisily gen grade = futuregrade 
replace grade = 0 if (futuregrade=="KI" & year==2013) 
gen prek = (regexm(futuregrade, ".*A") | regexm(futuregrade, ".*P")) 

duplicates tag mastid , gen(dup) 
duplicates drop mastid if dup>0 , force 

gen eslyes = regexm(eslserved, ".*Y") 
gen eslw = regexm(eslserved, ".*W") 
gen eslc = regexm(eslserved, ".*C") 	
gen esle = regexm(eslserved, ".*E") 
gen esln = (regexm(eslserved, ".*N") & `year'>2007) 
gen eslm = regexm(eslserved, ".*M") 
egen any_esl = rowmax(eslyes eslw eslc esle esln eslm) 
	
tab eslyes 
tab eslm 
tab eslc
tab eslw 
tab esle 
tab esln 
tab any_esl 


tempfile cms`year' 
	save `cms`year'' 
	
	} 

use `cms06' , clear 

foreach y in 07 08 09 10 11 12 13 { 
	append using `cms`y'' 
	} 
	
gen homeschl_s = strofreal(homeschl) 
gen currschool_s = strofreal(currschool) 

replace homeschl_s = regexs(2) if regexm(homeschl_s, "([0-9])([0-9][0-9][0-9])") 
replace currschool_s = regexs(2) if regexm(currschool_s, "([0-9])([0-9][0-9][0-9])") 
	
replace homeschl = real(homeschl_s) 
replace currschool = real(currschool_s) 
	
bysort mastid: egen ever_esl = max(any_esl) 

duplicates tag mastid grade , gen(repeat) 
bysort mastid: egen minyear = min(year) if repeat>0 
drop if minyear!=year & repeat>0 
drop repeat minyear 

gen frpl = . 
replace frpl = 0 if year<2011 
replace frpl = 1 if (frelig!="3" & (year==2008 | year==2009 | year==2010)) 
	
gen black = regexm(ethnic, ".*B") 
gen white = regexm(ethnic, ".*W") 
gen hispanic = regexm(ethnic, ".*S") 
gen other = (1 - black - white - hispanic) 

gen sibling = regexm(placementtype, ".*S") 
gen titleone = regexm(placementtype, ".*I") 
bysort homeschl year: egen anytitleone = max(titleone) 	

merge m:1 appch1 using `appch1' , gen(appch1merge) 
merge m:1 appch2 using `appch2' , gen(appch2merge) 
merge m:1 appch3 using `appch3' , gen(appch3merge) 

gen wonfirst = asschoice==appch1 
gen wonsecond = asschoice==appch2 
gen wonthird = asschoice==appch3 

gen appch1_link = appch1 if (appch1!=0 & appch1!=.) 
gen appch2_link = appch2 if (appch2!=0 & appch2!=.) 
gen appch3_link = appch3 if (appch3!=0 & appch3!=.) 
   
save CMS_Clean_Long.dta , replace 

